﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizPhongHoc
    {
        /// <summary>
        /// Lấy danh sách phòng theo CSĐT
        /// </summary>
        /// <returns></returns>
        public static List<PhongHoc> GetList(string guidIdCoSoDaoTao, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)

                using (var db2 = CEMS4UG_DHKHContext.GetContext())
                    return GetList(guidIdCoSoDaoTao, db2);
            else
                return db.PhongHocDbSet.Where(e => e.MaCoSoDaoTaoGUID == guidIdCoSoDaoTao).OrderBy(e => e.MaPhongHoc).ToList();
        }


        /// <summary>
        /// Lấy tất cả các phòng
        /// </summary>
        /// <returns></returns>
        public static List<PhongHoc> GetList(CEMS4UG_DHKHContext dbContext = null)
        {
            if (dbContext != null)
                return dbContext.PhongHocDbSet.OrderBy(e => e.MaPhongHoc).ToList();
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.PhongHocDbSet.OrderBy(e => e.MaPhongHoc).ToList();
            }
        }

        public static PhongHoc GetByID(string maPhongHoc)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.PhongHocDbSet.Where(e => e.MaPhongHoc == maPhongHoc).FirstOrDefault();
            }
        }

        //public static bool IsExistByID(string maPhongHoc)
        //{
        //    using (var db = CEMS4UG_DHKHContext.GetContext())
        //    {
        //        return db.PhongHocDbSet.Where(e => e.MaPhongHoc == maPhongHoc).FirstOrDefault() == null ? false : true;
        //    }
        //}

        /// <summary>
        /// Kiểm tra Mã có tồn tại hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByCode(string maPhongHoc)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.PhongHocDbSet.Where(e => e.MaPhongHoc == maPhongHoc).FirstOrDefault() == null ? false : true;
            }
        }

        /// <summary>
        /// Kiểm tra tên có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(object id)
        {
            throw new Exception();
        }

        public static CODE_RESULT_RETURN Add(PhongHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.PhongHocDbSet.Where(e => e.MaPhongHoc == obj.MaPhongHoc && e.MaCoSoDaoTaoGUID == obj.MaCoSoDaoTaoGUID).FirstOrDefault();
                if (_ph != null)
                    return CODE_RESULT_RETURN.MaTrung;
                db.PhongHocDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<PhongHoc>(nhatKy, null, obj.MaPhongHoc, LOAIDOITUONG.PHÒNG_HỌC, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<PhongHoc>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(PhongHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.PhongHocDbSet.Where(e => e.TenPhongHoc == obj.TenPhongHoc && e.MaCoSoDaoTaoGUID == obj.MaCoSoDaoTaoGUID && e.MaPhongHoc != obj.MaPhongHoc).FirstOrDefault();
                if (_ph != null)
                    return CODE_RESULT_RETURN.TenTrung;
                _ph = db.PhongHocDbSet.Where(e => e.MaPhongHoc == obj.MaPhongHoc).FirstOrDefault();
                if (_ph == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<PhongHoc>(nhatKy, _ph, _ph.MaPhongHoc, LOAIDOITUONG.PHÒNG_HỌC, LOAITHAOTAC.Chỉnh_sửa);
                _ph.SoLuongHocToiDa = obj.SoLuongHocToiDa;
                _ph.SoLuongThiToiDa = obj.SoLuongThiToiDa;
                _ph.TenPhongHoc = obj.TenPhongHoc;
                _ph.MaCoSoDaoTaoGUID = obj.MaCoSoDaoTaoGUID;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<PhongHoc>(nhatKy, _ph);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(PhongHoc obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _ph = db.PhongHocDbSet.Where(e => e.MaPhongHoc == obj.MaPhongHoc).FirstOrDefault();
                if (_ph == null)
                    return CODE_RESULT_RETURN.ThatBai;
                db.PhongHocDbSet.Remove(_ph);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<PhongHoc>(nhatKy, _ph, _ph.MaPhongHoc, LOAIDOITUONG.PHÒNG_HỌC, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<PhongHoc>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }

}
